package com.dianqu.weather.core.common;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

public class ClassUtils {

	public static List<Object> getObjects(String packageName) {
		ClassLoader classLoader = Thread.currentThread()
				.getContextClassLoader();
		String path = packageName.replace(".", "/");
		URL url = classLoader.getResource(path);
		List<Object> list = new ArrayList<Object>();
		try {
			File file = new File(URLDecoder.decode(url.getFile(), "utf-8"));
			for (File f : file.listFiles()) {
				Class clazz = Class.forName(packageName + "."
						+ f.getName().substring(0, f.getName().length() - 6));
				list.add(clazz.newInstance());
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return list;
	}

	public static void main(String[] args) {
		for (Object obj : getObjects("com.dianqu.weather.web.engine.impl")) {
			System.out.println(obj);
		}
	}
}
